import { Draw } from "../../../Draw/Draw";
import { IKonva } from "../../../Interface/IKonva";

export function lineControlDragStart(draw: Draw) {
  const stage = draw.getStage();

  // 获取当前画布上的元素，可吸附坐标
  const allShapes = draw.getKonvaGraph().getBaseGraph();

  const positionCache: IKonva.LinePositionCache[] = [];

  //   allShapes
  allShapes.forEach((shape) => {
    // 取 scale width height x y
    const { x, y, realWidth, realHeight, width, height, id } = shape.attrs;
    const w = realWidth || width;
    const h = realHeight || height;

    /**
     * 这样就能解析一个图形的四个中间点
     *    1
     *  4   2
     *    3
     */
    const points = [
      { id, order: 1, x: x + w / 2, y: y },
      { id, order: 2, x: x + w, y: y + h / 2 },
      { id, order: 3, x: x + w / 2, y: y + h },
      { id, order: 4, x: x, y: y + h / 2 },
    ];
    positionCache.push(...points);
  });

  stage.setAttrs({ positionCache });
}
